﻿using Edge.Repository.Model.ResistanceWelding;
using Mapster;
using Oliving.FormCommon.Oliving.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TMW.Edge.Common.Extensions;

namespace ChargingSystem4.EUc
{
    public partial class FormSelEquip : Form
    {
        public FormSelEquip()
        {
            InitializeComponent();
            //      this.TopMost = true;
            InitDGV();
            WinFormStyle.InitDataGridViewStyle(dataGridView1);
        }

        private void FormSelEquip_Load(object sender, EventArgs e)
        {
            GridData();
        }

        private void InitDGV()
        {

            this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "编号", DataPropertyName = "Id", Visible = false });
            this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "名称", DataPropertyName = "Name" });


            this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "设备ID", DataPropertyName = "Code" });

            this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "工位", DataPropertyName = "GongWei" });
            this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "软件版本", DataPropertyName = "RuanJianBanBen" });
            this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "焊接流量", DataPropertyName = "HanJieDianLiu" });
            this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "备注", DataPropertyName = "Remark" });
            Image imgUpdate = Image.FromFile(System.Windows.Forms.Application.StartupPath + "\\Context\\Image\\xz31.png");
            this.dataGridView1.Columns.Add(new DataGridViewImageColumn { HeaderText = "选择", Image = imgUpdate });

        }



        public void GridData()
        {
            // string key = this.textBox1.Text.Trim();
            Expression<Func<RW_Equipment, bool>> whereExpression = F => true;
            EquipmentService dal = new EquipmentService();
            //if (!string.IsNullOrEmpty(key))
            //{
            //    whereExpression = whereExpression.EdgeAnd(F => true);
            //}
            List<RW_Equipment> data = dal.Query(whereExpression);
            var list = data.Adapt<List<EquipmentDto>>();

            this.dataGridView1.DataSource = list;
        }

        public string _MyEquip = "";

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

            #region MyRegion

            if (e.Button == System.Windows.Forms.MouseButtons.Left && e.ColumnIndex > -1 && e.RowIndex > -1)
            //点击的是鼠标右键，并且不是表头            
            {
                for (int rowID = 0; rowID < this.dataGridView1.Rows.Count; rowID++)
                {
                    this.dataGridView1.Rows[rowID].Cells[e.ColumnIndex].Selected = false; ;
                }
                //右键选中单元格  
                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;

                int SelectUserID = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());

                int id = Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells[0].Value);

                if (e.ColumnIndex == 7)
                {
                    _MyEquip = (this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());

                    this.DialogResult = DialogResult.OK;
                    //FormKongZhiQiEdit userEdit = new FormKongZhiQiEdit(id);
                    //if (userEdit.ShowDialog() == DialogResult.OK)
                    //{
                    //    GridData();
                    //}
                }
                //else if (e.ColumnIndex == 7)
                //{

                //    #region 删除

                //    DialogResult dr = MessageBox.Show("确定要删除吗", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                //    if (dr == DialogResult.Yes)
                //    {
                //        EquipmentService dal = new EquipmentService();

                //        if (dal.DeleteById(id))
                //        {
                //            MessageBox.Show("已删除！");
                //            GridData();
                //        }
                //        else
                //        {
                //            MessageBox.Show("删除失败！");
                //        }
                //    }
                //    #endregion
                //}
            }

            #endregion

        }

        private void FormSelEquip_FormClosing(object sender, FormClosingEventArgs e)
        {
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
